package com.siwei.apply.service.impl;

import com.siwei.apply.domain.Jsgcghxk;
import com.siwei.apply.domain.Project;
import com.siwei.apply.domain.res.JsgcghxkRes;
import com.siwei.apply.domain.res.JsgcghxkSinglRes;
import com.siwei.apply.domain.vo.JsgcghxkUpdateVo;
import com.siwei.apply.domain.vo.JsgcghxkVo;
import com.siwei.apply.mapper.JsgcghxkMapper;
import com.siwei.apply.mapper.ProjectMapper;
import com.siwei.apply.service.JsgcghxkService;
import com.siwei.common.core.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.siwei.apply.common.Common.UserId;

// 新增导入：项目流程相关
import org.springframework.transaction.annotation.Transactional;
import com.siwei.apply.domain.ProjectWorkflow;
import com.siwei.apply.domain.Workflow;
import com.siwei.apply.mapper.ProjectWorkflowMapper;
import com.siwei.apply.mapper.WorkflowMapper;

/**
 * 建设工程规划许可 服务实现类
 */
@Service
public class JsgcghxkImpl implements JsgcghxkService {
    @Autowired
    private JsgcghxkMapper jsgcghxkMapper;
    @Autowired
    private ProjectMapper projectMapper;
    // 新增：项目流程相关Mapper
    @Autowired
    private ProjectWorkflowMapper projectWorkflowMapper;
    @Autowired
    private WorkflowMapper workflowMapper;

    @Override
    public Boolean isExit(String projectId) {
        return jsgcghxkMapper.isExit(projectId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String add(JsgcghxkVo jsgcghxkVo) {
        Jsgcghxk jsgcghxk = new Jsgcghxk();
        BeanUtils.copyProperties(jsgcghxkVo, jsgcghxk);
        jsgcghxk.generateId();
        jsgcghxk.setCreatorId(UserId);
        jsgcghxkMapper.add(jsgcghxk);
        // 添加项目流程（仿照YdbpImpl）
        Integer projectType = projectMapper.getProjectTypeById(jsgcghxk.getProjectId());
        Workflow workflow = workflowMapper.selectByProjectTypeAndTableName(projectType, "t_jsgcghxk");
        Workflow workflowPrev = workflowMapper.selectPrevByProjectTypeAndTableName(projectType, "t_jsgcghxk");
        String parentId = "";
        if (workflowPrev != null) {
            List<ProjectWorkflow> projectWorkflows = projectWorkflowMapper.selectByProjectIdAndNodeTableName(jsgcghxk.getProjectId(), workflowPrev.getTableName());
            if (projectWorkflows != null && !projectWorkflows.isEmpty()) {
                parentId = projectWorkflows.get(0).getNodeId();
            }
        }
        ProjectWorkflow projectWorkflow = new ProjectWorkflow();
        projectWorkflow.setId(jsgcghxk.getId());
        projectWorkflow.setParentId(parentId);
        projectWorkflow.setWorkflowId(workflow.getId());
        projectWorkflow.setProjectId(jsgcghxk.getProjectId());
        projectWorkflow.setNodeId(jsgcghxk.getId());
        projectWorkflow.setNodeTableName(workflow.getTableName());
        projectWorkflow.setWorkflowName(workflow.getTableName());
        projectWorkflowMapper.add(projectWorkflow);
        return jsgcghxk.getId();
    }

    @Override
    public JsgcghxkRes get(String projectId) {
        List<Jsgcghxk> jsgcghxkList = jsgcghxkMapper.get(projectId);
        Project project = projectMapper.get(projectId);

        JsgcghxkRes jsgcghxkRes = new JsgcghxkRes();
        jsgcghxkRes.setProjectId(projectId);
        jsgcghxkRes.setProjectName(project.getName());
        jsgcghxkRes.setProjectCode(project.getCode());
        jsgcghxkRes.setJsgcghxkList(jsgcghxkList);

        return jsgcghxkRes;
    }

    @Override
    public JsgcghxkSinglRes getById(String id) {
        Jsgcghxk record = jsgcghxkMapper.getById(id);
        if (record == null) {
            return null;
        }
        Project project = projectMapper.get(record.getProjectId());
        JsgcghxkSinglRes res = new JsgcghxkSinglRes();
        BeanUtils.copyProperties(record, res);
        if (project != null) {
            res.setProjectName(project.getName());
            res.setProjectCode(project.getCode());
        }
        return res;
    }

    @Override
    public void update(JsgcghxkUpdateVo jsgcghxkUpdateVo) {
        jsgcghxkMapper.update(jsgcghxkUpdateVo);
    }

    @Override
    public void updateHasOnchain(String id, Boolean hasOnchain) {
        jsgcghxkMapper.updateHasOnchain(id, hasOnchain);
    }
}
